﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Xml;

using System.Collections;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Reflection;
using System.Windows.Forms;

public class Common2
{
    public static string config_dir = Application.StartupPath + "/config";
    public static string config_file = config_dir + "/config.xml";
    public static string print_file=config_dir + "/printConfig.xml";
    //public static string config_dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config";
    //public static string config_file = config_dir.Replace(@"file:\", "") + "/config.xml";

    /// <summary>
    /// 修改数据集
    /// </summary>
    /// <param name="dt">数据源</param>
    /// <param name="field">请求字段</param>
    /// <param name="key">主键值</param>
    /// <param name="value">值</param>
    /// <returns></returns>
    public static DataTable ChangeDataTable(DataTable dt, string field, string key, string value)
    {
        //规格
        if ("sl" == field)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["cpbh"].ToString() == key)
                {
                    dr.BeginEdit();
                    dr[field] = value;//数量改变
                    dr["xj"] = MultNumber(decimal.Parse(value), decimal.Parse(dr["dj"].ToString()));//小计改变
                    dr.EndEdit();
                }
            }
        }
        return dt;
    }
    public static string MultNumber(decimal num, decimal dj)
    {
        decimal number = num * dj;
        return number.ToString("0.00");
    }
    /// <summary>

    /// 字符串截取方法

    /// </summary>

    /// <param name="str">截取字符</param>

    /// <param name="len">大于Len时,截取len</param>

    /// <returns></returns>

    public static string Substr(string str, int len)
    {

        if (!string.IsNullOrEmpty(str))
        {

            System.Text.RegularExpressions.Regex rChinese = new Regex(@"[\u4e00-\u9fa5]"); //验证中文

            Regex rEnglish = new Regex(@"^[A-Za-z0-9]+$"); //验证字母

            if (rChinese.IsMatch(str))
            {
                //中文
                return (str.Length > len) ? str.Substring(0, len) + "..." : str;

            }

            else if (rEnglish.IsMatch(str))
            {
                //英文
                return (str.Length > len * 2) ? str.Substring(0, len * 2) + "..." : str;
            }
            return (str.Length > len) ? str.Substring(0, len) + "..." : str;

        }

        return "";

    }
    public static string CutString(int start, int end, string str)
    {

        if (!string.IsNullOrEmpty(str))
        {

            Regex rChinese = new Regex(@"[\u4e00-\u9fa5]"); //验证中文

            Regex rEnglish = new Regex(@"^[A-Za-z0-9]+$"); //验证字母

            if (rChinese.IsMatch(str))
            {
                str = str.Substring(start, end);
            }

            else if (rEnglish.IsMatch(str))
            {
                str = str.Substring(start, end * 2);
            }
        }
        return str;
    }

    public static List<string> ArrayString(string str, int len)
    {
        List<string> strList = new List<string>();

        int start = 0;
        int end = start + len;

        while (true)
        {
            if (start >= str.Length)
                return strList;
            string temp = CutString(start, end, str);
            strList.Add(temp);
            start = end;
            end = end + 6;
            if (end >= str.Length)
            {
                end = str.Length;
            }
        }
    }

    public static SysConfig GetConfig()
    {
        if (!Directory.Exists(config_dir))
        {
            Directory.CreateDirectory(config_dir);
        }
        if (!File.Exists(config_file))
        {
            SysConfig config = new SysConfig();
            config.SourceAccount = "sa";
            config.SourceDatabaseName = "VideoWatchV3JJ";
            config.SourcePWD = "123456";
            config.SourceServer = @"192.168.10.11\SQL2008R2";

            //config.DescAccount = "sa";
            //config.DescDatabaseName = "VideoWatchV4";
            //config.DescPWD = "123456";
            //config.DescServer = @"192.168.10.11\SQL2008R2";

            //config.OracleAccount = "hztm";
            //config.OracleDataSource = "VQDV3MID";
            //config.OraclePWD = "hztm123";
            //config.OracleSQL = "select *from  ezview.tm_view_sbxx_info   ";
            string res = XmlUtil.Serializer(typeof(SysConfig), config);
            File.WriteAllText(config_file, res);

        }
        string config_xml = File.ReadAllText(config_file);
        return (SysConfig)XmlUtil.Deserialize(typeof(SysConfig), config_xml);
    }
    

    public static PrintConfig GetPrintConfig()
    {
        if (!Directory.Exists(config_dir))
        {
            Directory.CreateDirectory(config_dir);
        }
        if (!File.Exists(print_file))
        {
            PrintConfig config = new PrintConfig();
            config.PortName = "com1";
            config.Ls = null;
            string res = XmlUtil.Serializer(typeof(PrintConfig), config);
            File.WriteAllText(print_file, res);
        }
        string config_xml = File.ReadAllText(print_file);
        return (PrintConfig)XmlUtil.Deserialize(typeof(PrintConfig), config_xml);
    }

 

    /// <summary>
    /// 设置 config
    /// </summary>
    /// <param name="config"></param>
    /// <param name="config_file"></param>
    /// <returns></returns>
    public static int SetConfig(SysConfig config)
    {
        string res = XmlUtil.Serializer(typeof(SysConfig), config);
        File.WriteAllText(config_file, res);
        return 1;
    }


    /// <summary>
    /// 设置 config
    /// </summary>
    /// <param name="config"></param>
    /// <param name="config_file"></param>
    /// <returns></returns>
    public static int SetPrintConfig(PrintConfig config)
    {
        string res = XmlUtil.Serializer(typeof(PrintConfig), config);
        File.WriteAllText(print_file, res);
        return 1;
    }






    public int SetPosConfig(XmlModel config, string config_file)
    {
        string res = XmlUtil.Serializer(typeof(XmlModel), config);
        File.WriteAllText(config_file, res);
        return 1;
    }
    public XmlModel GetPosConfig(string config_dir, string config_file)
    {
        if (!Directory.Exists(config_dir))
        {
            Directory.CreateDirectory(config_dir);
        }
        if (!File.Exists(config_file))
        {
            XmlModel config = new XmlModel();



            string res = XmlUtil.Serializer(typeof(XmlModel), config);
            File.WriteAllText(config_file, res);

        }
        string config_xml = File.ReadAllText(config_file);
        return (XmlModel)XmlUtil.Deserialize(typeof(XmlModel), config_xml);
    }
    public XmlModel GetPosConfig()
    {
        string config_dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config";
        config_dir = config_dir.Replace(@"file:\", "");
        string config_file = config_dir + "/PosConfig.xml";



        if (!Directory.Exists(config_dir))
        {
            Directory.CreateDirectory(config_dir);
        }
        if (!File.Exists(config_file))
        {
            XmlModel config = new XmlModel();


            string res = XmlUtil.Serializer(typeof(XmlModel), config);
            File.WriteAllText(config_file, res);
        }
        string config_xml = File.ReadAllText(config_file);
        return (XmlModel)XmlUtil.Deserialize(typeof(XmlModel), config_xml);
    }

    public int SetPosConfig(List<PosFieldDetail> ls, string config_file)
    {
        string res = XmlUtil.Serializer(typeof(List<PosFieldDetail>), ls);
        File.WriteAllText(config_file, res);
        return 1;
    }
    public List<PosFieldDetail> GetPosConfig_1(string config_dir, string config_file)
    {
        if (!Directory.Exists(config_dir))
        {
            Directory.CreateDirectory(config_dir);
        }
        if (!File.Exists(config_file))
        {
            List<PosFieldDetail> ls = new List<PosFieldDetail>();


            string res = XmlUtil.Serializer(typeof(SysConfig), ls);
            File.WriteAllText(config_file, res);

        }
        string config_xml = File.ReadAllText(config_file);
        return (List<PosFieldDetail>)XmlUtil.Deserialize(typeof(List<PosFieldDetail>), config_xml);
    }

    public static void PrintDataTypeByObj(DataTable dt, Type type, string className)
    {
        PropertyInfo[] pis = type.GetProperties();
        foreach (DataColumn dc in dt.Columns)
        {
            foreach (PropertyInfo pi in pis)
            {
                if (pi.Name.ToLower() == dc.ColumnName.ToLower())
                {
                    string strType = type.GetProperty(pi.Name).PropertyType.ToString().ToLower();  //获取当前字段类型
                    if (dc.DataType.ToString().ToLower() != strType)
                    {
                        LogHelper.info(className + "->列名称:" + dc.ColumnName + ",类型:" + dc.DataType);
                    }
                    break;
                }
            }
        }
    }






}


